package fm.controller;

import fm.cache.AreaCache;
import fm.dto.NeiyiUser;
import fm.entity.Address;
import fm.exception.BizException;
import fm.mongoService.UserService;
import fm.util.CommonUtils;
import fm.web.MediaTypes;
import net.sf.ehcache.loader.CacheLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * web 注册控制器
 * Created by Javon on 2018/1/21.
 */
@Controller
@RequestMapping("/register")
public class RegisterController extends BaseController{
    @Autowired
    UserService userService;
    @RequestMapping("")
    public  String index(ModelMap modelMap, String err_msg){
        modelMap.put("province", AreaCache.getAllProvince());
        return  "portal/register";
    }

    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST,produces = MediaTypes.JSON_UTF_8)
    public Map userRegister(@RequestBody ModelMap modelMap){
        Map res = new HashMap();
        try{
            NeiyiUser user = new NeiyiUser();
            user.setOpenBank((String) modelMap.get("openBank"));
            user.setChairtMan((String) modelMap.get("chairtMan"));
            user.setCertCard((String) modelMap.get("certCard"));
            user.setBusListenerPic((String) modelMap.get("busListenerPic"));
            user.setPhone((String) modelMap.get("phone"));
            user.setCompanyName((String) modelMap.get("companyName"));
            user.setStName((String) modelMap.get("stName"));
            user.setUserAdd((String)modelMap.get("province"),(String)modelMap.get("city"),(String)modelMap.get("town"),(String)modelMap.get("detailAdd"));
            Address province = AreaCache.getAddress((String) modelMap.get("province"));
            Address city = AreaCache.getAddress((String) modelMap.get("city"));
            Address town = AreaCache.getAddress((String) modelMap.get("town"));
            user.setUserAddressName(CommonUtils.isEmpty(province)?"":province.getName(), CommonUtils.isEmpty(city)?"":city.getName(),CommonUtils.isEmpty(town)?"":town.getName());
            List companyPics = new ArrayList();
            companyPics.add((String)modelMap.get("companyPics1"));
            companyPics.add((String)modelMap.get("companyPics2"));
            companyPics.add((String)modelMap.get("companyPics3"));
            companyPics.add((String)modelMap.get("companyPics4"));
            user.setCompanyPics(companyPics);

            user.setUserContact((String) modelMap.get("userContact"));
            user.setIdCard((String) modelMap.get("idCard"));
            user.setIdCardImg1((String) modelMap.get("idCardImg1"));
            user.setIdCardImg2((String) modelMap.get("idCardImg2"));
            user.setUserType(Integer. valueOf((String) modelMap.get("userType")));
            user.setRegType("3");
            user.setReviewStatus(1);
            user.setUserDsc((String) modelMap.get("userDsc"));
            user.setUserLogo((String) modelMap.get("logo_img"));
            user.setTopLevel(0);
            user.setFirstUpdate(0);
             userService.addUser(user);
            res.put("data","");
            this.success(res);
        }catch (BizException ex){
            LOGGER.error("发生错误:",ex);
            this.failed(res,ex.getMessage());
        }catch (Exception ex){
            LOGGER.error("发生异常",ex);
            this.failed(res,"服务器发生了未知的错误，请联系管理员或者开发者解决!");
        }
        return res;
    }
}
